﻿#region copyright.anytao.com
// Copyright   : 2008, Anytao.com
// Author      : Anytao，http://www.anytao.com
// FileName    : Anytao.ISpace.DAL
// Release     : 2008/06/08 1.0
// Description : DAL for talk
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq;
using Anytao.ISpace.Entity;

namespace Anytao.ISpace.DAL
{
    public class TalkDAL : ITalk
    {
        ISpaceDataContext ctx = new ISpaceDataContext();

        /// <summary>
        /// Get all the talks
        /// </summary>
        /// <returns></returns>
        public IList<Talk> GetTalks()
        {
            return (from t in ctx.Talks
                    select t) as List<Talk>;
        }

        public IList<Talk> GetTalksByTime()
        {
            var talks = from t in ctx.Talks
                        orderby t.PostTime descending
                        select t;

            return talks.ToList<Talk>();

            //IList<Talk> tmp = talks.ToList<Talk>();
            //if (tmp != null)
            //    return tmp;
            //else
            //    return null;
        }

        public IList<Talk> GetTalksByUserID(int userID)
        {
            return (from t in ctx.Talks
                    where t.UserID == userID
                    select t) as List<Talk>;
        }

        public IList<Talk> GetTalksBySearch(string[] keywords)
        {
            return null;
        }

        public Talk GetTalk(int talkID)
        {
            return ctx.Talks.Single(t => t.TalkID == talkID);
        }

        public void Insert(Talk talk)
        {
            ctx.Talks.InsertOnSubmit(talk);
            ctx.SubmitChanges();
        }

        public void Delete(int talkID)
        {
            Talk talk = ctx.Talks.Single(t => t.TalkID == talkID);
            ctx.Talks.DeleteOnSubmit(talk);

            ctx.SubmitChanges();
        }

        public void DeleteByUserID(int userID)
        {
            var talks = from t in ctx.Talks
                        where t.UserID == userID
                        select t;

            foreach (var t in talks)
            {
                ctx.Talks.DeleteOnSubmit(t);
            }

            ctx.SubmitChanges();
        }

        public void Update(Talk talk)
        { 
        }
    }
}
